package cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill.vo.TaxBillPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill.vo.TaxBillRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.tax.taxbill.vo.TaxBillSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.tax.taxbill.TaxBillDO;
import cn.iocoder.yudao.module.erp.service.business.tax.taxbill.TaxBillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 发票类型")
@RestController
@RequestMapping("/erp/tax-bill")
@Validated
public class TaxBillController {

    @Resource
    private TaxBillService taxBillService;

    @PostMapping("/create")
    @Operation(summary = "创建发票类型")
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:create')")
    public CommonResult<Long> createTaxBill(@Valid @RequestBody TaxBillSaveReqVO createReqVO) {
        return success(taxBillService.createTaxBill(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新发票类型")
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:update')")
    public CommonResult<Boolean> updateTaxBill(@Valid @RequestBody TaxBillSaveReqVO updateReqVO) {
        taxBillService.updateTaxBill(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除发票类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:delete')")
    public CommonResult<Boolean> deleteTaxBill(@RequestParam("id") Long id) {
        taxBillService.deleteTaxBill(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得发票类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:query')")
    public CommonResult<TaxBillRespVO> getTaxBill(@RequestParam("id") Long id) {
        TaxBillDO taxBill = taxBillService.getTaxBill(id);
        return success(BeanUtils.toBean(taxBill, TaxBillRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发票类型分页")
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:query')")
    public CommonResult<PageResult<TaxBillRespVO>> getTaxBillPage(@Valid TaxBillPageReqVO pageReqVO) {
        PageResult<TaxBillDO> pageResult = taxBillService.getTaxBillPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaxBillRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发票类型 Excel")
    @PreAuthorize("@ss.hasPermission('erp:tax-bill:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaxBillExcel(@Valid TaxBillPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaxBillDO> list = taxBillService.getTaxBillPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "发票类型.xls", "数据", TaxBillRespVO.class,
                        BeanUtils.toBean(list, TaxBillRespVO.class));
    }

}